﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iHub.App_Code;

namespace iHub
{
    public partial class forgetStart : Form
    {
        public static forgetStart form1 = null;
        public String user = "";
        private AccountManager acct = new AccountManager();

        public forgetStart()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Start_Closing);
        }

        private void Start_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                login n = new login();
                n.Show();
            }
        }

        public String getUsername()
        {
            return user;
        }

        private void loginButton_Click(object sender, EventArgs e)
        {
            user = username.Text;
            String question;
            StringValidator validator = new StringValidator();

            try
            {
                if (validator.IsSafeString(user))
                {
                    question = acct.getQuestion(user);
                    if (question != "")
                    {
                        form1 = this;
                        this.Hide();

                        forgetQuest view = new forgetQuest(question);
                        view.ShowDialog();
                    }
                    else if (user == "")
                    {
                        DialogResult msg = MessageBox.Show("Please input your username.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                    else
                    {
                        DialogResult msg = MessageBox.Show("The username does not exist or it has been terminated. Please try again", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
                else
                {
                    MessageBox.Show("There are illegal characters present in the text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void forgetStart_Load(object sender, EventArgs e)
        {

        }

        private void back_Click(object sender, EventArgs e)
        {
            login n = new login();
            n.Show();
            this.Dispose();
        }
    }
}
